#!/usr/bin/env python3
"""
ArXiv Scraper Service - Daily Paper Harvesting System

A robust daily paper harvesting system using the OAI-PMH protocol for
a multi-user paper subscription platform.

Features:
- OAI-PMH client using sickle library
- Incremental updates with timestamp filtering
- Rate limiting (3-second delays)
- Robust error handling with retry logic
- Database integration with PostgreSQL
- Daily scheduling with state persistence
- Comprehensive logging and metrics

Author: ArXiv Scraper Agent 2
"""

import sys
import os
import logging
import argparse
from pathlib import Path

# Add the service modules to Python path
sys.path.insert(0, str(Path(__file__).parent))

from core.scraper_service import ArxivScraperService
from core.config_manager import ConfigManager
from utils.logger import setup_logging


def main():
    """Main entry point for the ArXiv scraper service."""
    parser = argparse.ArgumentParser(description='ArXiv Scraper Service')
    parser.add_argument('--config', '-c', default='config.yaml',
                        help='Configuration file path')
    parser.add_argument('--log-level', '-l', default='INFO',
                        choices=['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'],
                        help='Logging level')
    parser.add_argument('--dry-run', action='store_true',
                        help='Run in dry-run mode (no database writes)')
    parser.add_argument('--full-resync', action='store_true',
                        help='Perform full resynchronization from earliest date')
    parser.add_argument('--date-from', type=str,
                        help='Start date for scraping (YYYY-MM-DD format)')
    parser.add_argument('--date-until', type=str,
                        help='End date for scraping (YYYY-MM-DD format)')
    
    args = parser.parse_args()
    
    try:
        # Setup logging
        logger = setup_logging(level=args.log_level)
        
        # Load configuration
        config_manager = ConfigManager(args.config)
        config = config_manager.get_config()
        
        # Initialize scraper service
        scraper_service = ArxivScraperService(
            config=config,
            dry_run=args.dry_run
        )
        
        # Determine scraping mode
        if args.full_resync:
            logger.info("Starting full resynchronization...")
            results = scraper_service.full_resync()
        elif args.date_from or args.date_until:
            logger.info(f"Starting date-range scraping from {args.date_from} to {args.date_until}...")
            results = scraper_service.scrape_date_range(
                date_from=args.date_from,
                date_until=args.date_until
            )
        else:
            logger.info("Starting incremental scraping...")
            results = scraper_service.run_incremental_scrape()
        
        # Log results
        logger.info(f"Scraping completed successfully:")
        logger.info(f"  - Papers processed: {results.get('papers_processed', 0)}")
        logger.info(f"  - Papers stored: {results.get('papers_stored', 0)}")
        logger.info(f"  - Papers skipped (duplicates): {results.get('papers_skipped', 0)}")
        logger.info(f"  - Errors encountered: {results.get('errors', 0)}")
        logger.info(f"  - Execution time: {results.get('execution_time', 0):.2f} seconds")
        
        return 0
        
    except KeyboardInterrupt:
        logger.info("Scraping interrupted by user")
        return 1
    except Exception as e:
        logger.error(f"Fatal error: {e}", exc_info=True)
        return 1


if __name__ == '__main__':
    sys.exit(main())